﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Net;
using System.IO;
using System.Text;
using Model;
using System.Data.SqlClient;
using System.Configuration;

namespace Z_Ymian.Controllers
{
    public class HomeController : Controller
    {



        // GET: /Home/
        /// <summary>

        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 前台登陆
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }
        /// <summary>
        /// 前台登陆
        /// </summary>
        /// <returns></returns>
        public ActionResult Test()
        {
            return View();
        }

        ///// <summary>
        ///// 前台用户登录
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public int jsonLogin(string Name, string Pwd)
        //{
        //  //  DataSet ds = usersbll.Login(Name, Pwd);
        //    //if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count == 1)
        //    //{
        //    //    Session["Login_Nickname"] = ds.Tables[0].Rows[0][3];
        //    //    return 1;
        //    //}
        //    //else
        //    //{
        //    //    return 0;

        //    //}
        //    return 0;

        //}
        /// <summary>
        /// 试题
        /// </summary>
        /// <returns></returns>
        public ActionResult Questions()
        {
            return View();
        }
        /// <summary>
        /// 试题上传
        /// </summary>
        /// <returns></returns>
        public ActionResult Uploading()
        {
            return View();
        }
        /// <summary>
        /// 用户中心
        /// </summary>
        /// <returns></returns>
        public ActionResult UserCenter()
        {
            return View();
        }
        ///// <summary>
        ///// 我购买的文章
        ///// 用户中心
        ///// </summary>
        ///// <returns></returns>
        //public ActionResult UserCenter()
        //{
        //    return View();
        //}
        /// <summary>
        /// 我收藏的文章
        /// </summary>
        /// <returns></returns>
        public ActionResult company_orders()
        {
            return View();
        }
        /// <summary>
        /// 我收藏的文章详情
        /// </summary>
        /// <returns></returns>
        public ActionResult company_ordersInfor()
        {
            return View();
        }
        /// <summary>
        /// 测试用页面
        /// </summary>
        /// <returns></returns>
        public ActionResult CeShi()
        {
            return View();
        }

        /// <summary>
        /// 前台登陆
        /// </summary>
        /// <param name="Login_Name"></param>
        /// <param name="Login_Pwd"></param>
        /// <returns></returns>
        public string getLogin(string Login_Name, string Login_Pwd)
        {
            string aaa = PostDataGetHtml(@"http://localhost:32132/api/values/getjsonLogin?Login_Name=" + Login_Name + "&&Login_pwd=" + Login_Pwd + "");
            List<string> list = aaa.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Cast<string>().ToList<string>();
            if (list != null && list.Count > 0 && !list[0].ToUpper().Contains("NULL"))
            {
                var Login_Nickname1 = list[3].Substring(list[3].IndexOf(":") + 2);
                var Login_Nickname = Login_Nickname1.Substring(0, Login_Nickname1.LastIndexOf("\""));
                var Login_Id = list[0].Substring(list[0].IndexOf(":") + 1);
                Session["Login_Nickname"] = Login_Nickname;
                Session["Login_Id"] = Login_Id;
                aaa = Login_Nickname;
            }
            else
            {
                Session["Login_Nickname"] = null;
                Session["Login_Id"] = null;
                aaa = string.Empty;
            }
            return aaa;
        }

        /// <summary>
        /// 调用WEBapi
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static string PostDataGetHtml(string uri)
        {
            try
            {
                Uri uRI = new Uri(uri);
                HttpWebRequest req = WebRequest.Create(uRI) as HttpWebRequest;
                req.Method = "GET";
                req.KeepAlive = true;
                req.ContentType = "application/x-www-form-urlencoded";
                req.AllowAutoRedirect = true;
                HttpWebResponse wr = req.GetResponse() as HttpWebResponse;
                string xml = new StreamReader(wr.GetResponseStream(), Encoding.UTF8).ReadToEnd();
                return xml;

            }
            catch (Exception ex)
            {
                return "网络错误：" + ex.Message.ToString();
            }
        }

        /// <summary>
        /// 显示收藏的文章
        /// </summary>
        /// <returns></returns>
        public string GetRegard(int PageIndex, int PageSize)
        {
            int Uid = Convert.ToInt32(Session["Login_Id"]);
            string Json = PostDataGetHtml(@"http://localhost:32132/api/Regard/GetRegard/?Uid=" + Uid + "&&PageIndex=" + PageIndex + "&&PageSize=" + PageSize + "");
            return Json;
        }
        /// <summary>
        /// 显示收藏的文章详细信息
        /// </summary>
        /// <returns></returns>
        public string GetRegardInfor(int Id)
        {
            int Uid = Convert.ToInt32(Session["Login_Id"]);
            string Json = PostDataGetHtml(@"http://localhost:32132/api/Regard/GetRegardId?Uid=" + Uid + "&&Id=" + Id + "");
            return Json;
        }
        /// <summary>
        /// 发布经验文章
        /// </summary>
        /// <returns></returns>
        public ActionResult ArticlesExperience()
        {
            return View();
        }
        /// <summary>
        /// 显示收藏的文章
        /// </summary>
        /// <returns></returns>
        public string GetRegardALL(int PageIndex, int PageSize)
        {
            string Json = PostDataGetHtml(@"http://localhost:32132/api/Regard/GetRegardALL?PageIndex=" + PageIndex + "&&PageSize=" + PageSize + "");
            return Json;
        }
        /// <summary>
        /// 显示收藏的文章详细信息
        /// </summary>
        /// <returns></returns>
        public string GetRegardIdAll(int Id)
        {
            string Json = PostDataGetHtml(@"http://localhost:32132/api/Regard/GetRegardIdAll?Id=" + Id + "");
            return Json;
        }
        /// <summary>
        /// 显示面试经验文章
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowExperience()
        {
            return View();
        }
        /// <summary>
        /// 显示面试经验文章详细信息
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowExperienceInfor()
        {
            return View();
        }
        // <summary>
        // 判断文章是否已经收藏
        // </summary>
        // <param name="Id"></param>
        // <returns></returns>
        //public int AddRegard(int Id)
        //{
        //    int Count =Convert.ToInt32(PostDataGetHtml(@"http://localhost:32132/api/Regard/AddRegard?Id=" + Id + ""));
        //    return Count;
        //}



        /// <summary>
        /// 上传试题
        /// </summary>
        /// <param name="ii"></param>
        /// <param name="img">图片文件</param>
        /// <param name="aud">音频文件</param>
        /// <returns></returns>
        public ActionResult UploadingDo(Interview ii, HttpPostedFileBase img, HttpPostedFileBase aud)
        {
            if (img.ContentLength > 20 && aud.ContentLength > 50)
            {
                string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();


                //   System.Web.HttpFileCollection _file = System.Web.HttpContext.Current.Request.Files;
                if (img.ContentLength > 0 && aud.ContentLength > 0)
                {
                    string path = "";
                    string audpath = "";
                    //文件大小
                    long size = img.ContentLength;
                    long audsize = aud.ContentLength;
                    //文件类型
                    string type = img.ContentType;
                    string audtype = aud.ContentType;
                    //文件名
                    string name = img.FileName;
                    string audname = aud.FileName;
                    //文件格式
                    string _tp = System.IO.Path.GetExtension(name);
                    string _audtp = System.IO.Path.GetExtension(audname);

                    if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf")
                    {

                        if (_audtp.ToLower() == ".wav" || _audtp.ToLower() == ".mp3" || _audtp.ToLower() == ".wma" || _audtp.ToLower() == ".ogg" || _audtp.ToLower() == ".jpg")
                        {
                            //获取文件流
                            System.IO.Stream audstream = aud.InputStream;
                            //保存文件
                            string audsaveName = DateTime.Now.ToString("yyyyMMddHHmmss") + "aud" + _audtp;
                            //  string path = DataFactory.WFile.FileUploadPath + "/upload/area/" + saveName;
                            audpath = Server.MapPath("~/UploadFile/" + audsaveName);
                            aud.SaveAs(audpath);


                            //获取文件流
                            System.IO.Stream stream = img.InputStream;
                            //保存文件
                            string saveName = DateTime.Now.ToString("yyyyMMddHHmmss") + _tp;
                            //  string path = DataFactory.WFile.FileUploadPath + "/upload/area/" + saveName;
                            path = Server.MapPath("~/UploadFile/" + saveName);
                            img.SaveAs(path);


                            path = @"~/UploadFile/" + saveName;
                            audpath = @"~/UploadFile/" + audsaveName;
                            ii.interview_tapes = audpath;
                            ii.interview_image = path;
                            ii.Login_Nickname = Session["Login_Nickname"].ToString();
                            ii.User_id = Convert.ToInt32(Session["Login_Id"].ToString());

                            string sqlstr1 = "insert into Firm values('" + ii.Firm_Name + "','" + ii.Rated_Id + "')  select @@identity";


                            SqlConnection con = new SqlConnection(constring);

                            SqlCommand audcmd = new SqlCommand(sqlstr1, con);
                            con.Open();

                            int b = Convert.ToInt32(audcmd.ExecuteScalar());
                            string sqlstr = "insert into Interview values('" + ii.position_id + "','" + ii.interview_data + "','" + ii.interview_tapes + "','" + ii.interview_image + "','" + ii.interview_attached + "','1','0','" + b + "','" + ii.User_id + "','1')";
                            SqlCommand cmd = new SqlCommand(sqlstr, con);
                            int a = Convert.ToInt32(cmd.ExecuteNonQuery());
                            con.Close();

                            if (a > 0 && b > 0)
                            {
                                return Content("<script>alert('成功');location.href='/Home/Index'</script>");
                            }
                            else
                            {
                                return Content("<script>alert('失败');location.href='/Home/Index'</script>");
                            }
                        }
                        else
                        {
                            return Content("<script>alert('请上传音频文件');location.href='/Home/Uploading'</script>");
                        }




                    }
                    else
                    {
                        return Content("<script>alert('请上传图片文件');location.href='/Home/Uploading'</script>");
                    }




                }


            }
            else
            {
                return Content("<script>alert('太大了');location.href='/Home/Uploading'</script>");
            }
            return View();
        }


        public string SS(int id)
        {
            string Json = PostDataGetHtml(@"http://localhost:32132/api/values/getshowinterviewinfo/" + id);
            return Json;
        }
    }
}
